import type { Result } from '@/types/file'
import { sha256 } from 'js-sha256'

onmessage = e => {
  const { file } = e.data

  const reader = new FileReader()
  reader.readAsArrayBuffer(file)
  reader.onload = () => {
    if (reader.result) {
      const uint8View = new Uint8Array(reader.result as ArrayBuffer)
      let hash = sha256.create()
      hash.update(uint8View)
      const hex = hash.hex()
      const res: Result = {
        code: 200,
        msg: '成功',
        data: hex
      }
      postMessage(res)
    }
  }

  reader.onerror = () => {
    const res: Result = {
      code: 500,
      msg: '文件读取失败',
      data: null
    }
    postMessage(res)
  }
}
